package com.reservation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.entity.ExhibitionReservation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 展览预约Mapper接口
 */
@Mapper
public interface ExhibitionReservationMapper extends BaseMapper<ExhibitionReservation> {
    
    /**
     * 根据用户ID查询预约列表
     *
     * @param page   分页参数
     * @param userId 用户ID
     * @return 预约列表
     */
    @Select("SELECT er.* FROM exhibition_reservation er WHERE er.user_id = #{userId} AND er.deleted = 0 ORDER BY er.create_time DESC")
    IPage<ExhibitionReservation> selectPageByUserId(Page<ExhibitionReservation> page, @Param("userId") Long userId);
    
    /**
     * 根据展览ID查询预约列表
     *
     * @param page        分页参数
     * @param exhibitionId 展览ID
     * @return 预约列表
     */
    @Select("SELECT er.* FROM exhibition_reservation er WHERE er.exhibition_id = #{exhibitionId} AND er.deleted = 0 ORDER BY er.create_time DESC")
    IPage<ExhibitionReservation> selectPageByExhibitionId(Page<ExhibitionReservation> page, @Param("exhibitionId") Long exhibitionId);
    
    /**
     * 根据展览ID和日期查询当日预约人数
     *
     * @param exhibitionId   展览ID
     * @param reservationDate 预约日期
     * @return 预约人数
     */
    @Select("SELECT COALESCE(SUM(visitor_count), 0) FROM exhibition_reservation WHERE exhibition_id = #{exhibitionId} AND reservation_date = #{reservationDate} AND status = 'APPROVED' AND deleted = 0")
    Integer countVisitorsByExhibitionIdAndDate(@Param("exhibitionId") Long exhibitionId, @Param("reservationDate") LocalDate reservationDate);
    
    /**
     * 根据展览ID和状态查询预约列表
     *
     * @param page        分页参数
     * @param exhibitionId 展览ID
     * @param status      预约状态
     * @return 预约列表
     */
    @Select("SELECT er.* FROM exhibition_reservation er WHERE er.exhibition_id = #{exhibitionId} AND er.status = #{status} AND er.deleted = 0 ORDER BY er.create_time DESC")
    IPage<ExhibitionReservation> selectPageByExhibitionIdAndStatus(Page<ExhibitionReservation> page, @Param("exhibitionId") Long exhibitionId, @Param("status") String status);
}